package com.xiaoluyy.demo.common.util;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;

import javax.imageio.ImageIO;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.FormatException;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
 
public class ZxingCODE128Handler {
    /**
     * 条形码编码
     * 
     * @param contents
     * @param width
     * @param height
     */
    public static byte[] encode(String contents, int width, int height) {
        int codeWidth = 3 + // start guard
                (7 * 6) + // left bars
                5 + // middle guard
                (7 * 6) + // right bars
                3; // end guard
        codeWidth = Math.max(codeWidth, width);
        try {
        	ByteArrayOutputStream bos = new ByteArrayOutputStream();
            BitMatrix bitMatrix = new MultiFormatWriter().encode(contents,
                    BarcodeFormat.CODE_128, codeWidth, height, null);
 
            MatrixToImageWriter.writeToStream(bitMatrix, "png",
                    bos);
            
            return bos.toByteArray();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
 
    /**
     * 解析条形码
     * 
     * @param is
     * @return
     */
    public static String decode(InputStream is) {
        BufferedImage image = null;
        Result result = null;
        try {
            image = ImageIO.read(is);
            if (image == null) {
                System.out.println("the decode image may be not exit.");
            }
            LuminanceSource source = new BufferedImageLuminanceSource(image);
            BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
 
            result = new MultiFormatReader().decode(bitmap, null);
            return result.getText();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    
    public static int genChenkSumNo(CharSequence s) {
    	int length = s.length();
		if (length == 0) {
			return -1;
		}
		try {
			int sum = 0;
			for (int i = length - 2; i >= 0; i -= 2) {
				int digit = (int) s.charAt(i) - (int) '0';
				if (digit < 0 || digit > 9) {
					throw FormatException.getFormatInstance();
				}
				sum += digit;
			}
			sum *= 3;
			for (int i = length - 1; i >= 0; i -= 2) {
				int digit = (int) s.charAt(i) - (int) '0';
				if (digit < 0 || digit > 9) {
					throw FormatException.getFormatInstance();
				}
				sum += digit;
			}
			int gw = 10 - sum % 10;
			if (gw != 10) 
				return gw;
			else 
				return 0;
		} catch (FormatException fe) {
			fe.printStackTrace();
			return 0;
		}
    }
    
    public static boolean checkStandardUPCEANChecksum(CharSequence s) throws FormatException {
		int length = s.length();
		if (length == 0) {
			return false;
		}

		int sum = 0;
		for (int i = length - 2; i >= 0; i -= 2) {
			int digit = (int) s.charAt(i) - (int) '0';
			if (digit < 0 || digit > 9) {
				throw FormatException.getFormatInstance();
			}
			sum += digit;
		}
		sum *= 3;
		for (int i = length - 1; i >= 0; i -= 2) {
			int digit = (int) s.charAt(i) - (int) '0';
			if (digit < 0 || digit > 9) {
				throw FormatException.getFormatInstance();
			}
			sum += digit;
		}
		return sum % 10 == 0;
	}
    
}